﻿//#define TEST_PLAYER_PREFS

using UnityEngine.Assertions;

public sealed class Storage {

    private static Storage m_Instance;
    public static Storage Instance = m_Instance ??= new Storage();

    private Storage() { }

    public string RoleId { get; set; }

    public string AccountId { get; set; }

    //
    public void SetInt(string key, int value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(key));

#if TEST_PLAYER_PREFS || !UNITY_EDITOR && UNITY_WEBGL
#if WEIXINMINIGAME
        PlayerPrefs.SetInt(key, value);
#elif BYTEDANCEMINIGAME
        TTSDK.TT.PlayerPrefs.SetInt(key, value);
#else
        UnityEngine.PlayerPrefs.SetInt(key, value);
#endif
#else
        UnityEngine.PlayerPrefs.SetInt(key, value);
#endif
    }

    public void SetFloat(string key, float value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(key));

#if TEST_PLAYER_PREFS || !UNITY_EDITOR && UNITY_WEBGL
#if WEIXINMINIGAME
        PlayerPrefs.SetFloat(key, value);
#elif BYTEDANCEMINIGAME
        TTSDK.TT.PlayerPrefs.SetFloat(key, value);
#else
        UnityEngine.PlayerPrefs.SetFloat(key, value);
#endif
#else
        UnityEngine.PlayerPrefs.SetFloat(key, value);
#endif
    }

    public void SetString(string key, string value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(key));

#if TEST_PLAYER_PREFS || !UNITY_EDITOR && UNITY_WEBGL
#if WEIXINMINIGAME
        PlayerPrefs.SetString(key, value);
#elif BYTEDANCEMINIGAME
        TTSDK.TT.PlayerPrefs.SetString(key, value);
#else
        UnityEngine.PlayerPrefs.SetString(key, value);
#endif
#else
        UnityEngine.PlayerPrefs.SetString(key, value);
#endif
    }

    public int GetInt(string key, int @default = 0) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(key));

#if TEST_PLAYER_PREFS || !UNITY_EDITOR && UNITY_WEBGL
#if WEIXINMINIGAME
        if (!PlayerPrefs.HasKey(key)) {
            PlayerPrefs.SetInt(key, @default);
            return @default;
        }
        return PlayerPrefs.GetInt(key);
#elif BYTEDANCEMINIGAME
        if (!TTSDK.TT.PlayerPrefs.HasKey(key)) {
            TTSDK.TT.PlayerPrefs.SetInt(key, @default);
            return @default;
        }
        return TTSDK.TT.PlayerPrefs.GetInt(key);
#else
        if (!UnityEngine.PlayerPrefs.HasKey(key)) {
            UnityEngine.PlayerPrefs.SetInt(key, @default);
            return @default;
        }
        return UnityEngine.PlayerPrefs.GetInt(key);
#endif
#else
        if (!UnityEngine.PlayerPrefs.HasKey(key)) {
            UnityEngine.PlayerPrefs.SetInt(key, @default);
            return @default;
        }
        return UnityEngine.PlayerPrefs.GetInt(key);
#endif
    }

    public float GetFloat(string key, float @default = 0f) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(key));

#if TEST_PLAYER_PREFS || !UNITY_EDITOR && UNITY_WEBGL
#if WEIXINMINIGAME
        if (!PlayerPrefs.HasKey(key)) {
            PlayerPrefs.SetFloat(key, @default);
            return @default;
        }
        return PlayerPrefs.GetFloat(key);
#elif BYTEDANCEMINIGAME
        if (!TTSDK.TT.PlayerPrefs.HasKey(key)) {
            TTSDK.TT.PlayerPrefs.SetFloat(key, @default);
            return @default;
        }
        return TTSDK.TT.PlayerPrefs.GetFloat(key);
#else
        if (!UnityEngine.PlayerPrefs.HasKey(key)) {
            UnityEngine.PlayerPrefs.SetFloat(key, @default);
            return @default;
        }
        return UnityEngine.PlayerPrefs.GetFloat(key);
#endif
#else
        if (!UnityEngine.PlayerPrefs.HasKey(key)) {
            UnityEngine.PlayerPrefs.SetFloat(key, @default);
            return @default;
        }
        return UnityEngine.PlayerPrefs.GetFloat(key);
#endif
    }

    public string GetString(string key, string @default = "") {
        Assert.IsFalse(string.IsNullOrWhiteSpace(key));


#if TEST_PLAYER_PREFS || !UNITY_EDITOR && UNITY_WEBGL
#if WEIXINMINIGAME
        if (!PlayerPrefs.HasKey(key)) {
            PlayerPrefs.SetString(key, @default);
            return @default;
        }
        return PlayerPrefs.GetString(key);
#elif BYTEDANCEMINIGAME
        if (!TTSDK.TT.PlayerPrefs.HasKey(key)) {
            TTSDK.TT.PlayerPrefs.SetString(key, @default);
            return @default;
        }
        return TTSDK.TT.PlayerPrefs.GetString(key);
#else
        if (!UnityEngine.PlayerPrefs.HasKey(key)) {
            UnityEngine.PlayerPrefs.SetString(key, @default);
            return @default;
        }
        return UnityEngine.PlayerPrefs.GetString(key);
#endif
#else
        if (!UnityEngine.PlayerPrefs.HasKey(key)) {
            UnityEngine.PlayerPrefs.SetString(key, @default);
            return @default;
        }
        return UnityEngine.PlayerPrefs.GetString(key);
#endif
    }

    // RoleId
    public void SetIntWithRole(string key, int value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

        key = $"{key}_@{RoleId}";
        SetInt(key, value);
    }

    public void SetFloatWithRole(string key, float value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

        key = $"{key}_@{RoleId}";
        SetFloat(key, value);
    }

    public void SetStringWithRole(string key, string value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

        key = $"{key}_@{RoleId}";
        SetString(key, value);
    }

    public int GetIntWithRole(string key, int @default = 0) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

        key = $"{key}_@{RoleId}";
        return GetInt(key, @default);
    }

    public float GetFloatWithRole(string key, float @default = 0f) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

        key = $"{key}_@{RoleId}";
        return GetFloat(key, @default);
    }

    public string GetStringWithRole(string key, string @default = "") {
        Assert.IsFalse(string.IsNullOrWhiteSpace(RoleId));

        key = $"{key}_@{RoleId}";
        return GetString(key, @default);
    }

    // AccountId
    public void SetIntWithAccount(string key, int value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

        key = $"{key}_#{AccountId}";
        SetInt(key, value);
    }

    public void SetFloatWithAccount(string key, float value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

        key = $"{key}_#{AccountId}";
        SetFloat(key, value);
    }

    public void SetStringWithAccount(string key, string value) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

        key = $"{key}_#{AccountId}";
        SetString(key, value);
    }

    public int GetIntWithAccount(string key, int @default = 0) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

        key = $"{key}_#{AccountId}";
        return GetInt(key, @default);
    }

    public float GetFloatWithAccount(string key, float @default = 0f) {
        Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

        key = $"{key}_#{AccountId}";
        return GetFloat(key, @default);
    }

    public string GetStringWithAccount(string key, string @default = "") {
        Assert.IsFalse(string.IsNullOrWhiteSpace(AccountId));

        key = $"{key}_#{AccountId}";
        return GetString(key, @default);
    }
}
